import Mock from 'mockjs';

// 模拟登录接口
Mock.mock('/api/login', 'post', (options) => {
    const { userName, passWord } = JSON.parse(options.body);
    if (userName === 'test' && passWord === '123456') {
        return {
            success: true,
            message: '登录成功',
            data: {
                token: Mock.Random.guid()
            }
        };
    }
    return {
        success: false,
        message: '用户名或密码错误',
    };
});

// 模拟注册接口
Mock.mock('/api/register', 'post', (options) => {
    const { userName } = JSON.parse(options.body);
    if (userName === 'test') {
        return {
            success: false,
            message: '用户名已存在',
        };
    }
    return {
        success: true,
        message: '注册成功',
    };
});

// 模拟基本信息接口
export const getCityData = (cityName) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      if (!cityName) {
        resolve({
          basicInfo: {
            name: '默认果蔬',
            origin: '默认产地',
            variety: '默认品种',
            plantingDate: '2023-01-01',
            maturity: 50
          },
          barData: [60, 75, 80],
          pieData: [
            { value: 30, name: '小果' },
            { value: 70, name: '中果' },
            { value: 100, name: '大果' }
          ],
          cameraInfo: [
            { name: '默认镜头1', position: '未知位置', recognizedCount: Math.floor(Math.random() * 50) },
            { name: '默认镜头2', position: '未知位置', recognizedCount: Math.floor(Math.random() * 50) },
            { name: '默认镜头3', position: '未知位置', recognizedCount: Math.floor(Math.random() * 50) }
          ]
        });
      } else {
        const cityInfo = {
          '南宁': {
            basicInfo: {
              name: '南宁芒果',
              origin: '南宁市',
              variety: '台农1号',
              plantingDate: '2023-03-15',
              maturity: 75
            },
            barData: [65, 80, 70],
            pieData: [
              { value: 25, name: '小果' },
              { value: 80, name: '中果' },
              { value: 120, name: '大果' }
            ],
            cameraInfo: [
              { name: '镜头1', position: '果园东侧', recognizedCount: 15 },
              { name: '镜头2', position: '果园中央', recognizedCount: 23 },
              { name: '镜头3', position: '果园西侧', recognizedCount: 11 }
            ]
          },
          '柳州': {
            basicInfo: {
              name: '柳州蜜橘',
              origin: '柳州市',
              variety: '温州蜜柑',
              plantingDate: '2022-11-01',
              maturity: 65
            },
            barData: [70, 65, 85],
            pieData: [
              { value: 30, name: '小果' },
              { value: 75, name: '中果' },
              { value: 110, name: '大果' }
            ],
            cameraInfo: [
              { name: '镜头A', position: '果园入口处', recognizedCount: 8 },
              { name: '镜头B', position: '果园北侧', recognizedCount: 17 },
              { name: '镜头C', position: '果园南部', recognizedCount: 12 }
            ]
          },
          '贵港': {
            basicInfo: {
              name: '贵港荔枝',
              origin: '贵港市',
              variety: '妃子笑',
              plantingDate: '2023-05-01',
              maturity: 85
            },
            barData: [75, 85, 90],
            pieData: [
              { value: 20, name: '小果' },
              { value: 60, name: '中果' },
              { value: 100, name: '大果' }
            ],
            cameraInfo: [
              { name: '镜头X', position: '果园东南角', recognizedCount: 10 },
              { name: '镜头Y', position: '果园中心高点', recognizedCount: 20 },
              { name: '镜头Z', position: '果园西北角', recognizedCount: 14 }
            ]
          },
          '来宾': {
            basicInfo: {
              name: '来宾龙眼',
              origin: '来宾市',
              variety: '石硖',
              plantingDate: '2023-04-10',
              maturity: 60
            },
            barData: [60, 70, 65],
            pieData: [
              { value: 28, name: '小果' },
              { value: 90, name: '中果' },
              { value: 130, name: '大果' }
            ],
            cameraInfo: [
              { name: '主镜头', position: '果园灌溉区', recognizedCount: 18 },
              { name: '次镜头1', position: '果园南端', recognizedCount: 13 },
              { name: '次镜头2', position: '果园西端', recognizedCount: 9 }
            ]
          }
        };

        resolve(cityInfo[cityName] || {
          basicInfo: {
            name: '未知果蔬',
            origin: '未知产地',
            variety: '未知品种',
            plantingDate: '未知日期',
            maturity: 0
          },
          barData: [0, 0, 0],
          pieData: [
            { value: 0, name: '小果' },
            { value: 0, name: '中果' },
            { value: 0, name: '大果' }
          ],
          cameraInfo: [] // 如果城市不存在，返回空数组
        });
      }
    }, 500);
  });
};